/*
 *  atBudget Personal Finance Management application
 *
 *  Copyright 2011, Ilia Frenkel <frenkel.ilia@gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
	Accounts simple list is used in the main view to show the list of
	account names.	
 */
Ext.define('ATB.view.account.SimpleList' ,{
    extend: 'Ext.grid.Panel',
    alias : 'widget.accountsimplelist',
	store : 'Accounts',
    title : 'Accounts List',
	hideHeaders: true,
	viewConfig: {
		emptyText: '<p style="text-align:center;color:#999999;font-weight: '+
		'bold;font-size:120%;margin-top:2em">No accounts found.</p>',
		stripeRows: false
	},
	bodyCls: 'atb-accountsimplelist',

    initComponent: function() {
        this.columns = [
            {header: 'Name', dataIndex: 'name', flex: 1, tdCls: 'atb-simplelist-item'}
        ];

        this.callParent(arguments);
		
		//make sure we save selected account before reloading the store
		this.mon(this.store, {
			beforeLoad: this.saveSelection,
			scope: this
		});
    },
	
	/*
		Save current selection.
	*/
	saveSelection: function() {
		this.lastSelection = this.selModel.getSelection();
	},

	/*
		Restores previously saved selection after store is loaded.
	*/
	restoreSelection: function() {
		if (this.lastSelection.length > 0) {
			Ext.each(this.lastSelection, function(record) {
				var rec = this.store.getById(record.getId());
				if (rec) {
					this.selModel.select(rec, true, false);
				}
			}, this);
		}
	},

	/*
		Bind restoreSelection to the store onLoad event.
	*/
	onStoreLoad: function(){
		this.restoreSelection();
	}
});
